﻿using System.Collections.Generic;
using System.Linq;
using Coder.Workflow.Searcher;
using Identity;
using Microsoft.EntityFrameworkCore;

namespace Coder.Workflow.Stores.Ef
{
    public class WorkActivityStore<T> : IWorkActivityStore<T> where T : WorkflowDbContext
    {
        private readonly T _context;

        public WorkActivityStore(T context)
        {
            _context = context;
        }

        public IEnumerable<WorkActivity> GetByInstance(int intanceId)
        {
            return _context.WorkActivities.Include(f => f.DepositUser)
                .Include(f => f.WorkTask).ThenInclude(f => f.Role)
                .Where(f => f.ProcessInstance.Id == intanceId)
                .OrderBy(f => f.CreateTime)
                .ToList();
        }

        public IQueryable<WorkActivity> BuildQueryMyWorkActiviy(WorkActivitySearcher searcher)
        {
            var roleIds = searcher.Roles.Select(f => f.Id);
            bool isDelegateRole = roleIds.Contains(_context.DelegateAdminId());
            var query = _context.WorkActivities
                .Where(f =>
                    (isDelegateRole || roleIds.Contains(f.Role.Id) || searcher.UserId == f.DepositUser.Id)
                    && (searcher.Statuses == null || searcher.Statuses.Contains(f.Status))
                    && (searcher.WorkTask==null||f.WorkTask.Name==searcher.WorkTask)
                        && (searcher.ProcessType == null ||
                            searcher.ProcessType.Contains(f.ProcessInstance.WorkProcess.WorkflowType))
                    
                );
            return query;
        }

        public IQueryable<WorkActivity> InclideAll(IQueryable<WorkActivity> workActivities)
        {
            return workActivities
                .Include(f => f.WorkTask)
                .Include(f => f.DepositUser)
                .Include(f => f.ProcessInstance)
                .ThenInclude(f => f.User)
                .Include(f => f.ProcessInstance)
                .ThenInclude(f => f.WorkProcess);
        }

        /// <summary>
        ///     获取待处理
        /// </summary>
        /// <param name="searcher"></param>
        /// <param name="total"></param>
        /// <param name="executeWhere"></param>
        /// <param name="extentionsQuery"></param>
        /// <returns></returns>
        public IEnumerable<WorkActivity> GetMyCurrent(WorkActivitySearcher searcher,
            out int total)
        {
            var query = BuildQueryMyWorkActiviy(searcher);

            total = query.Count();
            var dataQuery = InclideAll(query)
                .Skip(searcher.GetSkip()).Take(searcher.GetTake());
            return dataQuery;
        }


        public WorkActivity GetById(int id)
        {
            //由于 workFlowInstance. 排序不正确，因此，要另外的方式解决

            var result = _context.WorkActivities.Include(f => f.DepositUser)
                .Include(f => f.WorkTask)
                .ThenInclude(f => f.Role)
                .Include(f => f.ProcessInstance)
                //.ThenInclude(f => f.WorkProcess)
                //.ThenInclude(f => f.Items)
                //.ThenInclude(f => f.Role)
                .FirstOrDefault(f => f.Id == id);

            return result;
        }
    }
}